package com.css.fxfzypg.modules.preassesstaskmanage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.cache.DivisionCache;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.modules.group.vo.GroupVo;
import com.css.fxfzypg.modules.preassesstaskmanage.entity.PreAssessTaskEntity;
import com.css.fxfzypg.modules.preassesstaskmanage.service.PreAssessTaskService;
import com.css.fxfzypg.modules.preassesstaskmanage.vo.PreAssessTaskGroupVo;
import com.css.fxfzypg.modules.preassesstaskmanage.vo.PreAssessVo;
import com.css.fxfzypg.modules.preassesstaskmanage.vo.PreAssessTaskVo;
import com.css.fxfzypg.modules.surveytaskmanage.vo.TaskVo;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/preAssessTask")
@Slf4j
public class PreAssessTaskController {
    @Autowired
    PreAssessTaskService preAssessTaskService;
    /**
     * 新建任务
     * @param preAssessTaskVo
     * @return
     */
    @SysLog("新建任务")
    @PostMapping("/save")
    public RestResponse saveTask(@RequestBody PreAssessTaskVo preAssessTaskVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String parentTaskId=preAssessTaskService.saveTask(preAssessTaskVo);
            jsonObject.put("message","保存成功");
            jsonObject.put("parentTaskId",parentTaskId);
            restResponse= RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="保存失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 新建子任务
     * @param subTask
     * @return
     */
    @SysLog("新建子任务")
    @PostMapping("/saveSubTask")
    public RestResponse saveSubTask(@RequestBody PreAssessTaskEntity subTask){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            preAssessTaskService.saveSubTask(subTask);
            jsonObject.put("message","新建成功");
            restResponse= RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="新建失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 折叠任务列表查询
     * @param taskVo
     * @return
     */
    @SysLog("任务列表查询")
    @GetMapping("/page")
    public RestResponse getPage(TaskVo taskVo, HttpServletRequest request){
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=preAssessTaskService.getPage(taskVo,curPage,pageSize);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 子任务列表查询
     * @param parentTaskId
     * @return
     */
    @GetMapping("/getSubTask")
    public RestResponse getSubTask(String parentTaskId){
        RestResponse response = null;
        try{
            List<PreAssessTaskEntity> subTaskList=preAssessTaskService.getSubTask(parentTaskId);
            response = RestResponse.succeed(subTaskList);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 查看任务详情
     * @param id
     * @return
     */
    @SysLog("查看任务详情")
    @GetMapping("/findById")
    public RestResponse findById(String id){
        RestResponse response = null;
        try{
            PreAssessTaskGroupVo preAssessTaskGroupVo=preAssessTaskService.findById(id);
            response = RestResponse.succeed(preAssessTaskGroupVo);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 修改任务
     * @param
     * @return
     */
    @SysLog("修改任务")
    @PostMapping("/update")
    public RestResponse updateTask(@RequestBody PreAssessTaskEntity preAssessTaskEntity){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            preAssessTaskService.updateTask(preAssessTaskEntity);
            jsonObject.put("message","修改成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 分配小组
     * @param preAssessTaskEntity
     * @return
     */
    @SysLog("分配小组")
    @PostMapping("/assignGroup")
    public RestResponse assignGroup(@RequestBody PreAssessTaskEntity preAssessTaskEntity){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            preAssessTaskService.assignGroup(preAssessTaskEntity);
            jsonObject.put("message","分配成功");
            restResponse= RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="分配失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 删除任务
     * @param
     * @return
     */
    @SysLog("删除任务")
    @PostMapping("/delete/{ids}")
    public RestResponse deleteTask(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] arr= ids.split(",");
            List<String> list = Arrays.asList(arr);
            preAssessTaskService.deleteTask(list);
            jsonObject.put("message","删除完成");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取父任务编号
     * @return
     */
    @GetMapping("/taskCode")
    public RestResponse getTaskCode(String taskYear){
        RestResponse restResponse=null;
        try {
            String taskCode = preAssessTaskService.getTaskCode(taskYear);
            restResponse= RestResponse.succeed(taskCode);
        } catch (Exception e) {
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取子任务编号
     * @return
     */
    @GetMapping("/subTaskCode")
    public RestResponse getSubTaskCode(String taskNum){
        RestResponse restResponse=null;
        try {
            String subtaskCode=preAssessTaskService.getSubTaskCode(taskNum);
            restResponse= RestResponse.succeed(subtaskCode);
        } catch (Exception e) {
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 查询所有父任务
     * @param
     * @return
     */
    @GetMapping("/getParentTask")
    public RestResponse getParentTask(){
        RestResponse response = null;
        try{
            List<PreAssessTaskEntity> parentTaskList=preAssessTaskService.getParentTask();
            response = RestResponse.succeed(parentTaskList);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查询省名称和id
     * @param taskDistrict
     * @return
     */
    @GetMapping("getCityIdTaskDistrict")
    public RestResponse getCityIdByName(String taskDistrict){
        return preAssessTaskService.getCityIdByName(taskDistrict);
    }


    /**
     * 关联抽样调查点
     * @param
     * @return
     */
    @GetMapping("saveSurveySamplingPoint")
    public RestResponse saveSurveySamplingPoint(String samplingSurveyPointId,String id){
        return preAssessTaskService.saveSurveySamplingPoint(samplingSurveyPointId,id);
    }
}
